package com.fh.util;

import com.alibaba.druid.util.StringUtils;
import com.fh.dao.DaoSupport;
import com.fh.model.loan.SysUser;
import com.fh.model.system.Organization;
import com.fh.model.system.Role;
import com.fh.model.system.User;
import com.fh.service.system.organization.OrganizationServiceImpl;
import com.fh.service.system.user.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dell、 on 2017/5/23.
 */
public class UserUtils {

    public static User getUser(){
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User userr = (User)session.getAttribute(Const.SESSION_USERROL);
        return userr;
    }
	
	public static User getRealUser(){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User userr = (User)session.getAttribute(Const.SESSION_USER);
		return userr;
	}
    
    public static Map<String,String> getCurrentRole(){
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return (Map<String,String>)session.getAttribute(Const.SESSION_QX);
    }

    /**
     * 设置数据权限
     * @param pageData
     * @param userService
     * @return
     * @throws Exception
     */
    public static PageData setDataRole(PageData pageData, UserService userService) {
        User user = getUser();
        Role role = user.getRole();
        List<User> list1=  new ArrayList<>();
         /*角色：1管理员，经理 就查所有
        *   组长 查组长下属组员的案件列表
        *   组员查组员自己的案件列表
        * */
        if(Const.ROLE_CSZZ.equals(role.getROLE_ID())){
            //组长  获取组长所有下属组员信息(包括组长自己的)
            try {
				list1 =userService.getChildrenUser(user.getUSER_ID());
				list1.add(user);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else if(Const.ROLE_CSY.equals(role.getROLE_ID())){
            //当前用户为催收员角色
            list1.add(user);
        }
        if(list1!=null&&list1.size()>0){
            Integer[] ids =new  Integer[list1.size()];
            for(int i=0;i<list1.size();i++){
                ids[i]=list1.get(i).getUSER_ID();
            }
            pageData.put("userIds",ids);
        }else{
            pageData.put("userIds",null);
        }
        return pageData;
    }

    /**
     * 设置组权限
     * @param pageData
     * @param organizationService
     * @return
     * @throws Exception
     */
    public static PageData setGroupRole(PageData pageData, OrganizationServiceImpl organizationService) {
        User user = getUser();
        Role role = user.getRole();
        Organization organization =  new Organization(user.getORGANIZATION_ID());
         /*角色：组长 所属组
        * */
        if(Const.ROLE_CSZZ.equals(role.getROLE_ID())){
            //组长  获取组长所在组
            try {
                organization =organizationService.findById(organization);
                pageData.put("overdueDuration",organization.getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return pageData;
    }
    
    /**
     * 委外公司-专员登陆
     * @param pageData
     * @param userService
     * @return
     */
    public static PageData setWWDataRole(PageData pageData,UserService userService){
    	User user = getUser();
        Role role = user.getRole();
    	
        if(Const.ROLE_WWGS.equals(role.getROLE_ID())){//委外公司
        	try {
        		String id=String.valueOf(user.getUSER_ID());
        		SysUser user1 = userService.getWWUserName(id);
				pageData.put("assignCompany", user1.getName());
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else if(Const.ROLE_WWZY.equals(role.getROLE_ID())){//委外专员
        	String callName=pageData.getString("callUser");
        	//对委外专员模糊查询的处理
        	if(!StringUtils.isEmpty(callName)){
        		pageData.put("callUser", callName);
        		pageData.put("callUser3", String.valueOf(user.getUSER_ID()));//只展示该委外催收专员的案子
        		pageData.put("callUser2", "");
        	}else{
        		pageData.put("callUser2", user.getUSER_ID());
        	}
        }
        
    	return pageData;
    }
}
